package cn.lanqiao.web.controller;

import cn.lanqiao.common.entity.Cart;
import cn.lanqiao.common.utile.Result;
import cn.lanqiao.common.vo.CartVo;
import cn.lanqiao.system.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {
    @Autowired
    private CartService cartService;
    //添加购物车
    @RequestMapping("/save")
    public Result save(@RequestBody Cart cart){
        System.out.println("收到的购物车信息："+cart);
        try {
            int i=cartService.save(cart);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误！");
    }
    //删除购物车
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            int i=cartService.delete(id);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误!");
    }
     //按用户id查询所有购物车
    @RequestMapping("/getAll")
    public Result getAll(@RequestParam Integer id){
        try {
            List<CartVo> list=cartService.getAll(id);
            return Result.ok(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误！");
    }
}
